<template>
    <div class="body_background" :style="GetWindowBackground">
        <div class="body_background padding-top10 ">
            <div class="add-inventory-box green-color middle-font-size"
                 @click="goAddGoodsView"
            >
                <span>+</span>
                <span class="padding-left">添加出库商品</span>
            </div>
            <div class="block-box items-start order-list-box" v-if="goodsList.length>0">
                <div class="big-font-size three-color">商品清单</div>
                <div class="order-list-item">
                    <div class="order-content-box" v-for="(item,index) in goodsList" :key="item.id">
                        <div class="big-small-circular" :class="item.choseFlag==true?'background':''"
                             @click="choseGoodsFun(item,index)"
                        >
                            <div class="small-circular" v-if="item.choseFlag==true"></div>
                        </div>
                        <img :src="item.imgUrl"/>
                        <div class="order-info-box">
                            <div class="main-color big-font-size">{{item.title}}</div>
                            <div class="goods-content">
                                <div>
                                    <div class="three-color small-font-size">分类：蔬菜</div>
                                    <div class="three-color small-font-size">规格：200g</div>
                                    <div class="three-color small-font-size">库存：200袋</div>
                                </div>
                                <!--购物车加减号-->
                                <div class="shop-cart-box">
                                    <div class="shop-cart-button green-color"
                                         @click="minusGoodsFun(item)"
                                    >-
                                    </div>
                                    <input type="number"
                                           v-model="item.num"
                                           class="shop-cart-input"
                                           @blur="inputBlurFun(item,item.num)"
                                    />
                                    <div class="shop-cart-button background"
                                         @click="addGoodsFun(item,false)"
                                    >+
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="block-box items-start marBottom" v-if="goodsList.length>0">
            <div class="big-font-size main-color">出库原因</div>
            <textarea class="note-content" placeholder="请输入出库原因" v-model="note"></textarea>
        </div>
        <div class="body_background create-button-box">
            <mt-button class="width40 middle-font-size"
                       @click="cancelClickFun"
            >取消
            </mt-button>
            <mt-button class="width40 middle-font-size background white-color"
                       @click="createInventoryFun"
            >提交
            </mt-button>
        </div>
    </div>
</template>

<script>
    import {Toast} from 'mint-ui';

    export default {
        name: "AddDeliveryGoods",
        data() {
            return {
                GetWindowBackground: {
                    'min-height': ''
                },
                goodsList: [],
                note: '备注',
            }
        },
        mounted() {
            this.getNewGoodsList();//重新整合商品数据
        },
        methods: {
            //重新整合商品数据
            getNewGoodsList() {
                var list = JSON.parse(localStorage.getItem('add_delivery_goods'));
                if(list){
                    for (var i = 0; i < list.length; i++) {
                        var item = list[i]
                        item.num = 1
                    }
                    this.goodsList = list
                }
                // localStorage.removeItem('add_delivery_goods');
            },

            //选商品
            choseGoodsFun(item) {
                item.choseFlag = !item.choseFlag
                item.num = item.choseFlag == true ? 1 : 0
            },

            //input失焦时间（商品数量填写失焦）
            inputBlurFun(item, num) {
                this.addGoodsFun(item, num)
            },

            //选商品 -
            minusGoodsFun(item) {
                if (item.num <= 0) {
                    Toast('不能再减啦')
                    return false
                }
                if (item.num == 1) {
                    item.choseFlag = false
                    item.num = --item.num
                } else {
                    item.num = --item.num
                }
            },

            //选商品 +
            addGoodsFun(item, num) {
                item.num = num == false ? ++item.num : Number(num)
                item.choseFlag = true
            },

            //跳转出库商品列表
            goAddGoodsView() {
                this.$router.push('/AddGoodsView?type=1')
            },

            //取消
            cancelClickFun() {
                localStorage.removeItem('add_delivery_goods');
                this.$router.push('/StockGoodsManage')
            },

            //提交
            createInventoryFun() {
                var list=this.goodsList;
                var array=[];
                for (var i = 0; i < list.length; i++) {
                    var item = list[i]
                    if(item.choseFlag){
                        array.push({
                            id:item.id,
                            num:item.num
                        })
                    }
                }
                var data={
                    arr:array,
                    note:this.note
                }
                console.log(data)
                localStorage.removeItem('add_delivery_goods');
                this.$router.push('/StockGoodsManage')
            },

            getHeight() {
                // 获取浏览器高度
                this.GetWindowBackground['min-height'] = window.innerHeight + 'px'
            },
        },
        // 生命周期 - 创建完成（可以访问当前this实例）
        created() {
            window.addEventListener('resize', this.getHeight) // 注册监听器
            this.getHeight() // 页面创建时先调用一次
        },
        // 生命周期 - 销毁完成
        destroyed() {
            window.removeEventListener('resize', this.getHeight)
        },
    }
</script>

<style lang="scss" scoped>

    .create-box {
        width: 100%;
        margin-top: 5px;
        li {
            width: 100%;
            height: 45px;
            display: flex;
            align-items: flex-end;
            div {
                margin-bottom: 7px;
            }
        }
    }

    .order-list-box {
        margin: 10px auto;
        .order-list-item {
            width: 100%;
            background: #fff;
            margin-top: 10px;
            border-radius: 4px;
            .order-top-box {
                width: 100%;
                height: 35px;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }
            .order-content-box {
                width: 100%;
                display: flex;
                align-items: center;
                padding: 7px 0;
                img {
                    width: 65px;
                    height: 65px;
                    margin-right: 10px;
                    border-radius: 4px;
                }
                .goods-content {
                    width: 100%;
                    display: flex;
                    justify-content: space-between;
                }
                .order-info-box {
                    width: calc(100% - 110px);
                    position: relative;
                    display: flex;
                    flex-direction: column;
                    justify-content: space-between;
                    text-align: left;
                }
                .goods-count {
                    margin-left: auto;
                    margin-top: 5px;
                }
            }
            .big-small-circular {
                width: 15px;
                height: 15px;
                border-radius: 50%;
                border: 1px solid #05c569;
                display: flex;
                justify-content: center;
                align-items: center;
                margin-right: 10px;
                .small-circular {
                    width: 7px;
                    height: 7px;
                    border-radius: 50%;
                    background: #fff;
                }
            }
        }
    }

    .add-inventory-box {
        width: 70%;
        height: 40px;
        line-height: 40px;
        background: #fff;
        border-radius: 6px;
        margin: auto;
        font-size: 14px;
        .padding-left {
            padding-left: 15px;
        }
    }

    .order-content-div {
        width: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: space-between;
        border-top: 1px dashed #D2D6DC;
        padding-top: 5px;
        margin-top: 15px;
        font-size: 15px;
        .content-item {
            width: 100%;
            min-height: 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
    }

    .note-content {
        width: calc(100% - 20px);
        border-radius: 6px;
        border: 1px solid #eef0f3;
        margin-top: 5px;
        text-align: left;
        font-size: 13px;
        padding: 10px;
    }

    .create-button-box {
        width: 100%;
        height: 70px;
        position: fixed;
        bottom: 0px;
        display: flex;
        justify-content: space-around;
        align-items: center;
        .width40 {
            width: 40%;
            border: 1px solid #05c569;
            color: #05c569;
            border-radius: 10px;
        }
    }

    .shop-cart-box {
        position: absolute;
        bottom: 15px;
        right: -10px;
        display: flex;
    }

</style>